package com.dzl.fee.service.impl;

import com.dzl.base.service.BaseServiceImpl;
import com.dzl.fee.dao.WelfareFeeUserSalesMapper;
import com.dzl.fee.pojo.WelfareFeeUserCustomersSalesRel;
import com.dzl.fee.pojo.WelfareFeeUserSales;
import com.dzl.fee.pojo.query.WelfareFeeUserSalesQuery;
import com.dzl.fee.pojo.vo.WelfareFeeUserSalesVo;
import com.dzl.fee.service.WelfareFeeUserCustomersSalesRelService;
import com.dzl.fee.service.WelfareFeeUserSalesService;
import com.dzl.util.EmptyUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 业务员信息表(WelfareFeeUserSales)表服务实现类
 *
 * @author wangjian
 * @date 2021-08-03 15:17:52
 */
@Service
public class WelfareFeeUserSalesServiceImpl extends BaseServiceImpl<WelfareFeeUserSales> implements WelfareFeeUserSalesService {

    @Autowired
    private WelfareFeeUserSalesMapper mapper;
    @Autowired
    private WelfareFeeUserCustomersSalesRelService welfareFeeUserCustomersSalesRelService;

    @Override
    public PageInfo<WelfareFeeUserSalesVo> getSalesVoListPage(WelfareFeeUserSalesQuery query) {
        if (EmptyUtils.isNotEmpty(query.getCustomersId())){
            WelfareFeeUserCustomersSalesRel welfareFeeUserCustomersSalesRel = new WelfareFeeUserCustomersSalesRel();
            welfareFeeUserCustomersSalesRel.setCustomerId(query.getCustomersId());
            List<WelfareFeeUserCustomersSalesRel> select = welfareFeeUserCustomersSalesRelService.select(welfareFeeUserCustomersSalesRel);
            StringBuffer salesIds = new StringBuffer();
            if (EmptyUtils.isNotEmpty(select)) {
                select.forEach(x -> {
                    salesIds.append(x.getSalesId()).append(",");
                });
            }
            query.setSalesIds(salesIds.toString());
            query.setPageNum(1);
            query.setPageSize(9999);
        }
        PageHelper.startPage(query);
        List<WelfareFeeUserSalesVo> list = mapper.getSalesVoList(query);
        PageInfo<WelfareFeeUserSalesVo> page = new PageInfo<WelfareFeeUserSalesVo>(list);
        return page;
    }

    @Override
    public Integer updateSales(WelfareFeeUserSales welfareFeeUserSales) {
        if (EmptyUtils.isEmpty(welfareFeeUserSales.getId())){
            mapper.insertSelective(welfareFeeUserSales);
        }else {
            mapper.updateByPrimaryKeySelective(welfareFeeUserSales);
        }
        return 1;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Integer updateSalesStatus(Integer id) {
        //删除业务员
        WelfareFeeUserSales welfareFeeUserSales = new WelfareFeeUserSales();
        welfareFeeUserSales.setId(id);
        welfareFeeUserSales.setStatus(0);
        mapper.updateByPrimaryKeySelective(welfareFeeUserSales);
        //删除关联关系
        WelfareFeeUserCustomersSalesRel welfareFeeUserCustomersSalesRel = new WelfareFeeUserCustomersSalesRel();
        welfareFeeUserCustomersSalesRel.setSalesId(id);
        welfareFeeUserCustomersSalesRelService.delete(welfareFeeUserCustomersSalesRel);
        return 1;
    }
}